package com.j1ay.forum.controller;

import com.j1ay.forum.model.pojo.po.Type;
import com.j1ay.forum.service.TypeService;
import com.j1ay.forum.model.pojo.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author J1ay
 * @Date 2021/12/15 20:33
 * @Description 问题分类 一对多
 */

@RestController
@RequestMapping("api/type")
@Api(value="Type Controller",tags={"类型类接口"})
public class TypeController {

    @Autowired(required = false)
    private TypeService typeService;

    @ApiOperation(value = "问题，热门分类 （该分类下文章最多）")
    @GetMapping("hot")
    public Result hot() {
        int limit = 3;
        return typeService.getHots(limit);
    }

    @ApiOperation(value = "获取所有分类列表")
    @GetMapping("all")
    public Result allTypeList() {
        return typeService.getAllTypeList();
    }


    @ApiOperation("增加分类")
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('is:admin')")
    public Result add(@RequestBody Type type){
        return typeService.add(type);
    }

    @ApiOperation("修改分类")
    @PutMapping("update")
    @PreAuthorize("hasAnyAuthority('is:admin')")
    public Result update(@RequestBody Type type){
        return typeService.update(type);
    }

    @ApiOperation("删除分类")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("hasAnyAuthority('is:admin')")
    public Result delete(@PathVariable("id") Long id){
        return typeService.delete(id);
    }
}
